<template>
  <page-header-wrapper>
    <a-card
      :bordered="true"
      :hoverable="true">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="姓名" prop="name">
                <a-input v-model="queryParam.name" allow-clear placeholder="请输入姓名" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="性别" prop="sex">
                <a-select placeholder="请选择性别" allow-clear>
                  <a-select-option v-for="(d, index) in sexOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="专业" prop="professional">
                  <a-select placeholder="请选择专业" allow-clear>
                    <a-select-option v-for="(d, index) in schoolProfessionalOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="学历" prop="degree">
                  <a-select placeholder="请选择学历" allow-clear>
                    <a-select-option v-for="(d, index) in eduLevelOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="状态" prop="jobState">
                  <a-select placeholder="请选择状态" allow-clear>
                    <a-select-option v-for="(d, index) in jobStateOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span
                :style="advanced && { float: 'right', overflow: 'hidden' } || {} "
                class="table-page-search-submitButtons">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
                <a style="margin-left: 8px" @click="toggleAdvanced">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 操作 -->
      <div class="table-operations">
        <a-button v-hasPermi="['platform:personnel:add']" type="primary" @click="$refs.createForm.handleAdd()">
          <a-icon type="plus" />
          新增
        </a-button>
        <a-button
          v-hasPermi="['platform:personnel:edit']"
          :disabled="single"
          type="primary"
          @click="$refs.createForm.handleUpdate(undefined, ids)">
          <a-icon type="edit" />
          修改
        </a-button>
        <a-button v-hasPermi="['platform:personnel:remove']" :disabled="multiple" type="danger" @click="handleDelete">
          <a-icon type="delete" />
          删除
        </a-button>
        <a-button
          :loading="loading"
          :style="{float: 'right'}"
          icon="reload"
          shape="circle"
          type="dashed"
          @click="getList" />
      </div>
      <!-- 增加修改 -->
      <create-form
        ref="createForm"
        :sexOptions="sexOptions"
        :jobStateOptions="jobStateOptions"
        :eduLevelOptions="eduLevelOptions"
        :schoolProfessionalOptions="schoolProfessionalOptions"
        :schoolOptions="schoolOptions"
        :postOptions="postOptions"
        @ok="getList"
      />
      <!-- 数据展示 -->
      <a-table
        :columns="columns"
        :data-source="list"
        :loading="loading"
        :pagination="false"
        :bordered="true"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :scroll="{ x: true }"
        :size="tableSize"
        rowKey="personnelId">
        <!--        <span slot="name" slot-scope="text, record">-->
        <!--          <a @click="$refs.viewForm.handleView(record, undefined)" v-if="checkPermi(['platform:student:query'])">{{ record.name }}</a>-->
        <!--          <span v-else>{{ record.name }}</span>-->
        <!--        </span>-->
        <span slot="sex" slot-scope="text, record">
          {{ record.sex === '0' ? '男' : (record.sex === '1' ? '女' : '未知') }}
        </span>
        <span slot="jobState" slot-scope="text, record">
          {{ jobStateFormat(record.jobState) }}
        </span>
        <span slot="school" slot-scope="text, record">
          {{ schoolFormat(record.school) }}
        </span>
        <span slot="degree" slot-scope="text, record">
          {{ eduLevelFormat(record.degree) }}
        </span>
        <span slot="professional" slot-scope="text, record">
          {{ schoolProfessionalFormat(record.professional) }}
        </span>
        <span slot="operation" slot-scope="text, record">
          <a v-hasPermi="['platform:student:edit']" @click="$refs.createForm.handleUpdate(record, undefined)">
            修改
          </a>
          <a-divider v-hasPermi="['platform:student:remove']" type="vertical" />
          <a style="color:red" v-hasPermi="['platform:student:remove']" @click="handleDelete(record)">
            删除
          </a>
        </span>
      </a-table>
      <!-- 分页 -->
      <a-pagination
        :current="queryParam.pageNum"
        :page-size="queryParam.pageSize"
        :showTotal="total => `共 ${total} 条`"
        :total="total"
        class="ant-table-pagination"
        show-quick-jumper
        show-size-changer
        @change="changeSize"
        @showSizeChange="onShowSizeChange"
      />
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { listPersonnel, delPersonnel } from '@/api/platform/personnel'
import { datas } from '@/api/platform/post'
import CreateForm from './modules/CreateForm'

export default {
  name: 'Personnel',
  components: {
    CreateForm
  },
  data () {
    return {
      // 岗位列表
      postOptions: [],
      // 性别列表
      sexOptions: [],
      // 工作状态列表
      jobStateOptions: [],
      // 教育水平列表
      eduLevelOptions: [],
      // 大学专业列表
      schoolProfessionalOptions: [],
      // 学校列表
      schoolOptions: [],
      list: [],
      selectedRowKeys: [],
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      ids: [],
      loading: false,
      total: 0,
      // 查询参数
      queryParam: {
        userId: undefined,
        deptId: undefined,
        layoutImages: undefined,
        name: undefined,
        sex: undefined,
        birthDate: undefined,
        home: undefined,
        live: undefined,
        degree: undefined,
        jobState: undefined,
        school: undefined,
        startDate: undefined,
        endDate: undefined,
        professional: undefined,
        evaluation: undefined,
        pageNum: 1,
        pageSize: 10
      },
      columns: [
        {
          title: '主键',
          dataIndex: 'personnelId',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '姓名',
          dataIndex: 'name',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '性别',
          dataIndex: 'sex',
          scopedSlots: { customRender: 'sex' },
          ellipsis: true,
          align: 'center'
        },
        {
          title: '出生日期',
          dataIndex: 'birthDate',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '求职意向',
          dataIndex: 'postName',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '工作状态',
          dataIndex: 'jobState',
          scopedSlots: { customRender: 'jobState' },
          ellipsis: true,
          align: 'center'
        },
        {
          title: '居住地',
          dataIndex: 'live',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '最高学历',
          dataIndex: 'degree',
          scopedSlots: { customRender: 'degree' },
          ellipsis: true,
          align: 'center'
        },
        {
          title: '毕业院校',
          dataIndex: 'school',
          scopedSlots: { customRender: 'school' },
          ellipsis: true,
          align: 'center'
        },
        {
          title: '就读专业',
          dataIndex: 'professional',
          scopedSlots: { customRender: 'professional' },
          ellipsis: true,
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
          align: 'center'
        }
      ],
      // 查看参数
      viewForm: {
        name: undefined,
        sex: undefined,
        birthDate: undefined,
        home: undefined,
        live: undefined,
        degree: undefined,
        jobState: undefined,
        school: undefined,
        startDate: undefined,
        endDate: undefined,
        professional: undefined,
        evaluation: undefined
      }
    }
  },
  filters: {},
  created () {
    // 加载性别列表
    this.getDicts('sys_user_sex').then(response => {
      this.sexOptions = response.data
    })
    // 加载工作状态列表
    this.getDicts('job_state').then(response => {
      this.jobStateOptions = response.data
    })
    // 教育教育水平列表
    this.getDicts('edu_level').then(response => {
      this.eduLevelOptions = response.data
    })
    // 加载大学专业列表
    this.getDicts('school_professional').then(response => {
      this.schoolProfessionalOptions = response.data
    })
    // 加载学校列表
    this.getDicts('school').then(response => {
      this.schoolOptions = response.data
    })
    // 岗位列表
    this.getPostDatas()
    this.getList()
  },
  computed: {},
  watch: {},
  methods: {
    /** 性别字典翻译 */
    sexFormat (val) {
      return this.selectDictLabel(this.sexOptions, val)
    },
    /** 工作状态字典翻译 */
    jobStateFormat (val) {
      return this.selectDictLabel(this.jobStateOptions, val)
    },
    /** 教育水平字典翻译 */
    eduLevelFormat (val) {
      return this.selectDictLabel(this.eduLevelOptions, val)
    },
    /** 大学专业字典翻译 */
    schoolProfessionalFormat (val) {
      return this.selectDictLabel(this.schoolProfessionalOptions, val)
    },
    /** 大学字典翻译 */
    schoolFormat (val) {
      return this.selectDictLabel(this.schoolOptions, val)
    },
    /** 获取岗位列表 */
    getPostDatas () {
      datas().then(response => {
        this.postOptions = response.data
      })
    },
    /** 查询人员设置列表 */
    getList () {
      this.loading = true
      listPersonnel(this.queryParam).then(response => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParam = {
        userId: undefined,
        deptId: undefined,
        layoutImages: undefined,
        name: undefined,
        sex: undefined,
        birthDate: undefined,
        home: undefined,
        live: undefined,
        degree: undefined,
        jobState: undefined,
        school: undefined,
        startDate: undefined,
        endDate: undefined,
        professional: undefined,
        evaluation: undefined,
        pageNum: 1,
        pageSize: 10
      }
      this.handleQuery()
    },
    onShowSizeChange (current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    changeSize (current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.personnelId)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const that = this
      const personnelIds = row.personnelId || this.ids
      this.$confirm({
        title: '删除提示',
        content: row.name ? '确定要删除' + row.name + '的数据吗？' : '确定要删除选中的数据吗？',
        onOk () {
          return delPersonnel(personnelIds)
            .then(() => {
              that.onSelectChange([], [])
              that.getList()
              that.$message.success(
                '删除成功',
                3
              )
            })
        },
        onCancel () {
        }
      })
    }
  }
}
</script>
<style lang='less' scoped>

</style>
